package com.oscar.ehome.view.pageview;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.oscar.ehome.R;
import com.oscar.ehome.base.fragment.BaseFragment;
import com.oscar.ehome.bean.pagebean.MemberBean;
import com.oscar.ehome.presenter.PagePresenter;
import com.oscar.ehome.utils.pullrefresh.PullToRefreshBase;
import com.oscar.ehome.utils.pullrefresh.PullToRefreshListView;
import com.oscar.ehome.utils.system.SystemConfigFactory;
import com.oscar.ehome.utils.util.ToastUtil;
import com.oscar.ehome.utils.view.progressview.ProgressFrameLayout;
import com.oscar.ehome.view.loginview.LoginActivity;
import com.oscar.ehome.view.pageview.adapter.MembernoAdapter;
import com.oscar.ehome.view.pageview.view.MembersListView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by juxing
 * author: MengWanhe
 * date: On 2017/7/21
 * 未认证
 */

public class MembernoApproveFragment extends BaseFragment<MembersListView, PagePresenter> implements
        PullToRefreshBase.OnRefreshListener, MembersListView, MembernoAdapter.OnMembersClickslister {

    @BindView(R.id.member_noapprove_listview)
    PullToRefreshListView memberNoapproveListview;

    @BindView(R.id.member_no_fragment)
    ProgressFrameLayout memberall_no_fragment;
    private View view;
    private int page = 1;//页数
    int count = 0;//条数
    PagePresenter presenter;
    String token = "";

    List<MemberBean.MemberMessageBean> list = new ArrayList<>();
    MembernoAdapter adapter;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_membernoapproe, null);
        ButterKnife.bind(this, view);
        initView();
        initData();
        return view;
    }

    @Override
    protected PagePresenter createPresenter() {
        presenter = new PagePresenter(this);
        return presenter;
    }

    public void initView() {
        token = SystemConfigFactory.getInstance(getActivity()).getSystemConfig().getToken();

        memberNoapproveListview.setPullLoadEnabled(true);
        memberNoapproveListview.setOnRefreshListener(this);
        adapter = new MembernoAdapter(getActivity(), list);
        memberNoapproveListview.setAdapter(adapter);
        adapter.setOnMembersClicksListener(this);
    }

    /**
     * 加载数据
     */
    public void initData() {
        presenter.getMemberlist(token, "0");
        showLoading();
    }

    @Override
    public void getMembersList(MemberBean result) {
        dismissLoading();
        memberNoapproveListview.onPullDownRefreshComplete();
        memberNoapproveListview.onPullUpRefreshComplete();
        memberNoapproveListview.setHasMoreData(false);
        if (result != null&&result.getCount()>0) {
            if (page == 1) {
                list.clear(); // 将原有的内容清空
            }
            list = result.getList();
            count = result.getCount();
            adapter.setLists(list);
        }else{
            memberNoapproveListview.setVisibility(View.GONE);
            memberall_no_fragment.setNoResult();
        }
    }

    @Override
    public void erroy() {
        dismissLoading();
        memberNoapproveListview.onPullDownRefreshComplete();
        memberNoapproveListview.onPullUpRefreshComplete();
        ToastUtil.show(getActivity(), getString(R.string.erry));
    }

    @Override
    public void erry(String result) {
        dismissLoading();
        memberNoapproveListview.onPullDownRefreshComplete();
        memberNoapproveListview.onPullUpRefreshComplete();
        ToastUtil.show(getActivity(), result);
        memberNoapproveListview.setVisibility(View.GONE);
        memberall_no_fragment.setNoResult();
        if (result.contains("过期")) {
            getActivity().getSharedPreferences("appbase", Context.MODE_PRIVATE).edit().clear().commit();
            SystemConfigFactory.getInstance(getActivity()).getSystemConfig().SetLogin(false);
            SystemConfigFactory.getInstance(getActivity()).getSystemConfig().setIsFirst(true);
            Intent intent = new Intent(getActivity(), LoginActivity.class);
            startActivity(intent);
            getActivity().finish();
        }
    }

    @Override
    public void onPullDownToRefresh(PullToRefreshBase refreshView) {
        page = 1;
        initData();
    }

    @Override
    public void onPullUpToRefresh(PullToRefreshBase refreshView) {

    }

    @Override
    public void OnClickMems(int postion) {
        if (list.size() > 0) {
            if (list.get(postion) != null) {
                if (list.get(postion).getMobile() != null && !"".equals(list.get(postion).getMobile())) {
                    Intent intent = new Intent(Intent.ACTION_DIAL);
                    Uri data = Uri.parse("tel:" + list.get(postion).getMobile());
                    intent.setData(data);
                    startActivity(intent);
                }
            }
        }
    }
}
